package com.speechb.common;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

public class  FileUtils {
    public static String saveFile(MultipartFile imgFile, String savePath, String dbPath)throws Exception {
        StringBuilder sb=new StringBuilder();
        String fileName = UUID.randomUUID().toString()
                + imgFile.getOriginalFilename();
        System.out.println(imgFile.getSize());
        System.out.println(imgFile.getName());
        System.out.println(fileName);
        File targetFile = new File(savePath, fileName);
        // 判断路径是否存在，如果不存在就创建一个
        if (!targetFile.getParentFile().exists()) {
            targetFile.getParentFile().mkdirs();
        }
        // 保存
        imgFile.transferTo(new File(sb.append(savePath).append(File.separator).append(fileName).toString()));
        sb.delete(0,sb.length());
        return sb.append(dbPath).append("/").append(fileName).toString();
    }
}
